<?php
namespace app\open\controller;
use think\Controller;

class Base extends Controller
{
    private $pri = [];

    public function _initialize(){
        //验证是否登录
        if( !session('?admin.id') ){
            request()->isAjax() ? $this->layJson(1, '您还没有登录，请在登录之后进行操作') : $this->redirect('Show/login');
        }

        //验证session是否有效
        if(time()-session('admin.start_time') > config('session.expire') ){
            session_destroy();
            request()->isAjax() ? $this->layJson(1, '您的登录时长已超过一周，请重新登录') : $this->error('您的登录时长已超过一周，请重新登录', 'Show/login');
        }

        $skinName = cookie('skin_name') ? cookie('skin_name') : '';
        $this->assign('skin_name', $skinName);
    }

    //lay-ui的格式返回
    protected function layJson($code=0, $msg='', $data=[]){
        $json['code'] = $code;
        $json['msg'] = $msg;
        $json['data'] = $data;
        echo json_encode($json);exit;
    }

    protected function layTable($code=0, $msg='', $data=[], $count=0, $params=[]){
        $json['code'] = $code;
        $json['msg'] = $msg;
        $json['data'] = $data;
        $json['count'] = $count;
        count($params) && $json['params'] = $params;
        echo json_encode($json);exit;
    }
}